package com.zt.english.activity.video

import android.media.MediaPlayer
import android.view.DragEvent
import android.view.View
import android.widget.SeekBar
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import cn.jzvd.*
import com.zt.english.R
import com.zt.english.activity.audio.YouDaoRepository
import com.zt.english.adapter.SubtitleRecycleAdapter
import com.zt.english.bean.SubtitlesBean
import com.zt.english.databinding.ActivityVideoPlayBinding
import com.zt.english.utils.GlideUtil
import com.zt.english.utils.SrtFileParasUtil
import com.zt.english.utils.SystemUtil
import com.zt.english.utils.ToasUtils
import com.zt.english.views.RecyclerViewListDivider
import com.zt.mvvm.common.log.LoggerUtil
import com.zt.mvvm.viewmodel.BaseViewModel
import java.util.ArrayList

class VideoPlayViewModel : BaseViewModel<VideoPlayRepository,ActivityVideoPlayBinding>() {
    private val mActivity by lazy {
        mLifecycleOwner as VideoPlayActivity
    }
    //上一个字幕读取的位置
    var lastPosition:Int = 0
    //字幕数据
    private var datas: ArrayList<SubtitlesBean>?=null

    override fun initView() {
        mBinding.jzVideo.setUp("https://static.esdict.cn/VideoPool/1946c1b7-5ba3-11eb-9f05-00505686c5e6/data/868c99df-b860-439f-adaa-8dcf6c907704.mp4", "拜登演讲")
        GlideUtil.loadImageView("http://p1.music.126.net/8d7Y0x8AzAE0kjc3eLhrAA==/109951165339281295.jpg?param=640y300",mBinding.jzVideo.posterImageView)
    }

    override fun initData() {
        val srt = mActivity.assets.open("CHSEN_拜登演讲.srt")
        datas = SrtFileParasUtil.parasSrt2List(srt)
        if(datas!=null){
            //设置布局管理器
            val manager = LinearLayoutManager(mActivity)
            manager.orientation = LinearLayoutManager.VERTICAL
            mBinding.recycleview.layoutManager = manager
            //设置分割线
            val divider = RecyclerViewListDivider(
                mActivity,
                LinearLayoutManager.HORIZONTAL,
                SystemUtil.dp2px(15f),
                ContextCompat.getColor(mActivity, R.color.color_main_background)
            )
            mBinding.recycleview.addItemDecoration(divider)
            //设置适配器
            val adapter = SubtitleRecycleAdapter(SubtitleRecycleAdapter.PlayTypeEnum.VIDEO,mActivity, datas!!, mRepository)
            mBinding.recycleview.adapter = adapter
            //item点击事件
            adapter.itemClickListener = object : SubtitleRecycleAdapter.ItemClickListener{
                override fun itemClick(position: Int, bean: SubtitlesBean) {
                    //跳转到指定位置播放
                    mBinding.jzVideo.mediaInterface.seekTo(bean.start.toLong())
                }
            }
            //设置视频seekbar进度控制回掉
//            mBinding.jzVideo.progressBar.max = 11*60*1000 + 22 * 1000
//            mBinding.jzVideo.progressBar.max = mBinding.jzVideo.mediaInterface.duration.toInt()
            mBinding.jzVideo.progressBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
                override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                    LoggerUtil.v("--onProgressChanged--")
                }
                override fun onStartTrackingTouch(seekBar: SeekBar?) {
                    LoggerUtil.v("--onStartTrackingTouch--")
                }
                override fun onStopTrackingTouch(seekBar: SeekBar?) {
                    LoggerUtil.v("--onStopTrackingTouch--")
                    //更新列表item播放位置
//                    if (seekBar != null) {
//                        lastPosition = searchSubPosition(seekBar.progress)
//                        adapter.read(lastPosition)
//
//                        LoggerUtil.v(lastPosition.toString() +"  "+seekBar.progress+" "+mBinding.jzVideo.mediaInterface.duration.toInt())
//                    }
                }
            })
        }
    }
    /**
     * 采用二分法去查找当前应该播放的字幕
     *
     * @param list 全部字幕
     * @param key 播放的时间点
     * @return 该字幕在集合中的位置
     */
    fun searchSubPosition(key: Int): Int{
        var start = 0
        var end: Int = datas!!.size- 1
        while (start <= end) {
            val middle = (start + end) / 2
            if (key < datas!![middle].start) {
                if (key > datas!![middle].end) {
                    return middle
                }
                end = middle - 1
            } else if (key > datas!![middle].end) {
                if (key < datas!![middle].start) {
                    return middle
                }
                start = middle + 1
            } else if (key >= datas!![middle].start && key <= datas!![middle].end) {
                return middle
            }
        }
        return lastPosition
    }
}